/**
 * UI分页列表控件
 * @author wyajun[wyajun@slof.com]
 */
function PaginationGrid(o){
	this.editor = o;
}
/**
 * 获取选中的第一条记录
 */
PaginationGrid.prototype.getSelected = function(){
	var $editor = jQuery(this.editor);
	var id = $editor.attr("id");
	return jQuery('#'+id).datagrid('getSelected');
}
/**
 * 获取选中的所有记录
 */
PaginationGrid.prototype.getSelections = function(){
	var $editor = jQuery(this.editor);
	var id = $editor.attr("id");
	return jQuery('#'+id).datagrid('getSelections');
}
/**
 * 增加查询参数
 */
PaginationGrid.prototype.putParameter = function(key,value){
	var $editor = jQuery(this.editor);
	var id = $editor.attr("id");
	var options = jQuery('#'+id).datagrid('options');
	var parames = options.queryParams;
//	alert(parseJSONString(options.queryParams));
	jQuery.extend(parames, eval("({\""+ key+"\":\""+ value+"\"})"));
//	var p = {'"'+key+'"':value};
//	jQuery.extend(parames, p);
	options.queryParams = parames;
//	alert(parseJSONString(options.queryParams));
	return this;
}

/**
 * 重新查询
 */
PaginationGrid.prototype.reload = function(){
	var $editor = jQuery(this.editor);
	var id = $editor.attr("id");
	var options = jQuery('#'+id).datagrid('options');
	options.pageNumber = 1;
//	alert(parseJSONString(options.queryParams));
	jQuery('#'+id).datagrid('reload');
	var pager = jQuery('#'+id).datagrid('getPager');
	var pageNumField = pager.find("input.pagination-num");
	pageNumField.val("1");
}
/**
 * 初始化分页列表
 */
PaginationGrid.prototype.onReady = function(){
    var $editor = jQuery(this.editor);
	var options = new Object();
	//nowrap：换行
	options.nowrap = false;
	//斑马色
	options.striped = true;
	//分页标识
	options.pagination = true;
	//选中行
	options.singleSelect = false;
	//id;标识
	var id = $editor.attr("id");
	//url:列表数据json获取的URL
	var url = $editor.attr("url");
	if (url && jQuery.trim(url) != "") {
		options.url = url;	
	}
	var data = $editor.attr("data");
	if (data && jQuery.trim(data) != "") {
		options.data = data;	
	}
	//分页列表宽度；不存在则充满父容器
	var width = $editor.attr("width");
	if(width && jQuery.trim(width) != ""){
		options.width=parseInt(width);
	}
	//分页列表高度度，不存在则充满父容器
	var height = $editor.attr("height");
	if(height && jQuery.trim(height) != ""){
		options.height=parseInt(height);
	}
	//pageList;//分页时每页记录数可选择的范围；默认[10,20,30,40,50]
	var pageList = $editor.attr("pageList");
	if(pageList && jQuery.trim(pageList) != ""){
		options.pageList=eval(pageList);
	}
	//pageSize;分页时每页显示的记录数；默认10条
	var pageSize = $editor.attr("pageSize");
	if(pageSize && jQuery.trim(pageSize) != ""){
		options.pageSize=pageSize;
	}
	//queryParams;执行URL的参数集合。格式name1:value1,name2:value2
	var queryParams = $editor.attr("queryParams");
	if(queryParams && jQuery.trim(queryParams) != ""){
		options.queryParams=eval("("+queryParams+")");
	}
	//rownumbers;是否显示行号
	var rownumbers = $editor.attr("rownumbers");
	if(rownumbers && jQuery.trim(rownumbers) != ""){
		options.rownumbers=rownumbers;
	} else {
		options.rownumbers=true;
	}
	//idField;隐藏的列对应数据集合的ID名称
	var idField = $editor.attr("idField");
	if(idField && jQuery.trim(idField) != ""){
		options.idField=idField;
	}
	//onSelectPage;//分页响应函数参数为pageNumber、pageSize（页码号，每页显示记录数）
	var onSelectPage = $editor.attr("onSelectPage");
	var columns = [];
	//var a = "[";
	//确定分页列表的表头
	var $trs = $editor.find("tr");
	$trs.each(function(i){
		//a = a + "[";
		var $tr = jQuery(this);
		columns[i] = [];
		var $tds = $tr.children();
		$tds.each(function(j){
			var $td = jQuery(this);
			var fieldObj = new Object();
			//title:分页列表头显示名称
			var title = $td.attr("title");
			if(title){
				fieldObj.title = title;
			}
			//field:分页列表头对应数据集合的属性名称
			var field = $td.attr("field");
			if(field){
				fieldObj.field = field;
			}
			//width:分页列表头宽度
			var width = $td.attr("width");
			if(width){
				fieldObj.width = width;
			}
			//rowspan:设置分页列表头跨几行
			var rowspan = $td.attr("rowspan");
			if(rowspan){
				fieldObj.rowspan = rowspan;
			}
			//colspan:设置分页列表头跨几列
			var colspan = $td.attr("colspan");
			if(colspan){
				fieldObj.colspan = colspan;
			}
			//align:分页列表对应列的排版方式align
			var align = $td.attr("align");
			if(align){
				fieldObj.align = align;
			} else {
				fieldObj.align = "center";
			}
			//checkbox:分页列表头对应列是否是checkbox列
//			var checkbox = $td.attr("checkbox");
//			if(checkbox){
//				fieldObj.checkbox = checkbox;
//			}
			var fieldType = $td.attr("type");
			if (fieldType) {
				fieldObj.fieldType = fieldType;
			}
			var clickFn = $td.attr("clickFn");
			if (clickFn) {
				fieldObj.clickFn = clickFn;
			}			
			//formatterFn:数据对应列的值自定义处理处理；参数为value返回值为要显示的内容如：'<span style="color:red">'+value+'</span>'
			var formatterFn = $td.attr("formatterFn");
			if(formatterFn){
				fieldObj.formatter = function(value, rowdate){
					try{
						return window[formatterFn](value,rowdate);
					}catch(err){
						return value;
					}					
				};
			}
			//a = a+"," + parseJSONString(fieldObj)
			columns[i][j]=fieldObj;
		});
		//a = a + "]"
	});
	options.columns=columns;
//	a = a + "]";
//	alert(a);
	//删除子节点
	$editor.empty();
	//alert(parseJSONString(options));
	//alert($editor.attr("id"))
	//执行分页列表组件函数
	using('datagrid', function(){
		jQuery("#"+id).datagrid(options);
		//onSelectPage;分页响应函数参数为pageNumber、pageSize（页码号，每页显示记录数）
		//var p = jQuery("#"+id).datagrid('getPager');			
	});
	
}